package edu.upf.opencities.web.action;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import com.opensymphony.xwork2.ActionSupport;

import edu.upf.opencities.datamodel.Entry;
import edu.upf.opencities.web.manager.impl.EntryManager;

public class OSNAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String serviceId;
	private String password;
	private String sourceId;
	private String unit;
	private String date;
	private String extraInfo;
	private String latitude;
	private String longitude;
	private String entryText;
	private File entry;
	private String entryFileName;
	private String entryContentType;
	private String fileData = "";
	private String message;
	
	/*IVAN AQUI TINES QUE METER LOS DIFERENTES CAMPOS DEL
	FORMULARIO. ES DECIR LOS CAMPOS QUE TIENE UNA ENTRY.XML/JSON 
	private String id, private String sourceId, ...
	Luego tienes que hacer los getter y setters*/
	
	
	public String upload(){

		try{
			
			//here we obtain the date from the system
			GregorianCalendar cal = new GregorianCalendar();
			cal.setTime(new Date());
			XMLGregorianCalendar date = DatatypeFactory.newInstance().
												newXMLGregorianCalendar(cal);
			
			//Here we create the Entry object
			Entry entryObj = new Entry();
			entryObj.setExtraInfo(extraInfo);
			entryObj.setSourceId(sourceId);
			entryObj.setUnit(unit);
			entryObj.setLatitude(latitude);
			entryObj.setLongitude(longitude);
			entryObj.setDate(date);
			
			//Here we read from the file if is needed
			if (entry != null){
				BufferedReader br = new BufferedReader(new FileReader(entry));
				String aux;
				while ((aux = br.readLine()) != null) {
					fileData += aux + "\n";
				}
				entryObj.setValue(fileData);
			} else {
				fileData = entryText;
				entryObj.setValue(fileData);
			}
			
			//Here we call the EntryManager to send the received information
			EntryManager manager = new EntryManager();
			String status = manager.sendEntry(serviceId, password, entryObj);
			if(status.equals("200") || status.equals("201")){
				setMessage("SUCCESS");
				return "SUCCESS";
			}
			else{
				 setMessage("ERROR");
		         return "ERROR";
			}
			
	      }catch(Exception e){
	    	  setMessage("ERROR");
	          e.printStackTrace();
	          return "ERROR";
	      }
	}


	public String getServiceId() {
		return serviceId;
	}

	public void setServiceId(String serviceId) {
		this.serviceId = serviceId;
	}


	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSourceId() {
		return sourceId;
	}

	public void setSourceId(String sourceId) {
		this.sourceId = sourceId;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getExtraInfo() {
		return extraInfo;
	}

	public void setExtraInfo(String extraInfo) {
		this.extraInfo = extraInfo;
	}

	public String getLatitude() {
		return latitude;
	}

	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}

	public String getLongitude() {
		return longitude;
	}

	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}

	public String getEntryText() {
		return entryText;
	}

	public void setEntryText(String entryText) {
		this.entryText = entryText;
	}

	public File getEntry() {
		return entry;
	}

	public void setEntry(File entry) {
		this.entry = entry;
	}

	public String getEntryFileName() {
		return entryFileName;
	}

	public void setEntryFileName(String entryFileName) {
		this.entryFileName = entryFileName;
	}

	public String getEntryContentType() {
		return entryContentType;
	}

	public void setEntryContentType(String entryContentType) {
		this.entryContentType = entryContentType;
	}

	public String getFileData() {
		return fileData;
	}

	public void setFileData(String fileData) {
		this.fileData = fileData;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}



}